package reduce.fz.com.reduce.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;


import com.alibaba.fastjson.TypeReference;

import java.util.HashMap;

import de.greenrobot.event.EventBus;
import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.TargetSet;
import reduce.fz.com.reduce.bean.Team;
import reduce.fz.com.reduce.bean.TeamDynamic;
import reduce.fz.com.reduce.bean.User;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.custom.LoadingDialog;
import reduce.fz.com.reduce.custom.MaterialDialog;
import reduce.fz.com.reduce.custom.MaterialDialogBuilder;
import reduce.fz.com.reduce.event.PubDynamicEvent;
import reduce.fz.com.reduce.service.TeamService;
import reduce.fz.com.reduce.service.preference.UserPreference;
import reduce.fz.com.reduce.util.InputMethodUtil;
import reduce.fz.com.reduce.util.ServiceBuilder;
import reduce.fz.com.reduce.util.TeamDynamicUtil;

public class TargetSettingActivity extends BaseToolBarActivity implements View.OnClickListener {
    //目标编辑框
    private EditText tvTarget;

    //private TextView mAllTime;
    //每天分钟数
    //private TextView minEveryDay;
    //需要坚持天数
   // private TextView mKeepDays;
    //完成按钮
    private TextView mNext;

    private Team team;

    private MaterialDialog dialog;
    private TeamService teamService = ServiceBuilder.builderTeamService(null);

    public TargetSettingActivity() {
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_target_setting);
        setToolbarTitle("目标设定");
        team = (Team) getIntent().getSerializableExtra("team");
        initWidget();
    }

    @Override
    public void initWidget() {
        tvTarget = (EditText) findViewById(R.id.tv_target);
       // minEveryDay = (TextView) findViewById(R.id.tv_min_everyday);
       // mKeepDays = (TextView) findViewById(R.id.tv_keep_days);
        mNext = (TextView) findViewById(R.id.tv_next);
        //mAllTime = (TextView) findViewById(R.id.tv_all_time);
       // minEveryDay.setOnClickListener(this);
        mNext.setOnClickListener(this);
        //mKeepDays.setOnClickListener(this);
    }

    /**
     * 总时间设定
     */
    public void keepday() {
        View view = LayoutInflater.from(this).inflate(R.layout.view_min_select, null);
        final EditText et = (EditText) view.findViewById(R.id.et_min);
        et.setInputType(EditorInfo.TYPE_CLASS_PHONE);
        final TextView confirm=(TextView)view.findViewById(R.id.tv_confirm);
        final TextView cancel=(TextView)view.findViewById(R.id.tv_cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dialog.dismiss();
            }
        });
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // mKeepDays.setText(et.getText().toString());
               // calAlltime();
                dialog.dismiss();
            }
        });
        if (dialog != null && dialog.isShowing()) return;
        dialog = MaterialDialogBuilder.getBuilder(this).build();
        dialog.setView(view);
        dialog.show();
        dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        InputMethodUtil.reuestFocusAndShowInput(et);


    }

    public void minEveryDay() {
        View view = LayoutInflater.from(this).inflate(R.layout.view_min_select, null);
        final EditText et = (EditText) view.findViewById(R.id.et_min);
        et.setInputType(EditorInfo.TYPE_CLASS_PHONE);
        final TextView confirm=(TextView)view.findViewById(R.id.tv_confirm);
        final TextView cancel=(TextView)view.findViewById(R.id.tv_cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dialog.dismiss();
            }
        });
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // minEveryDay.setText(et.getText().toString());
                //calAlltime();
                dialog.dismiss();
            }
        });
        if (dialog != null && dialog.isShowing()) return;
        dialog = MaterialDialogBuilder.getBuilder(this).build();
        dialog.setView(view);
        dialog.show();
        dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        InputMethodUtil.reuestFocusAndShowInput(et);
    }

    /*public void calAlltime() {
        String a=mKeepDays.getText().toString();
        String e=minEveryDay.getText().toString();
        if (TextUtils.isEmpty(a) || TextUtils.isEmpty(e)||"0".equals(a)||"0".equals(e))
            return;
        Integer day = Integer.parseInt(mKeepDays.getText().toString());
        Integer every = Integer.parseInt(minEveryDay.getText().toString());
        Integer alltime = day*every;
        mAllTime.setText(alltime+"");
    }*/

    @Override
    public void initListener() {


    }

    public void forwordFollow() {
        //final Long keepdays = Long.parseLong(mKeepDays.getText().toString());
        //final Long mineveryday = Long.parseLong(minEveryDay.getText().toString());
        String desc = tvTarget.getText().toString();
        final TargetSet set = new TargetSet();
        set.teamid = team.teamid;
        //set.minEveryDay = mineveryday;
        //set.keepDays = keepdays;
        set.desc = desc;
        Intent intent=new Intent(this,TargetFollowActivity.class);
        Bundle bundle=new Bundle();
        bundle.putSerializable("targetset",set);
        intent.putExtras(bundle);
        startActivity(intent);


    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
          //  case R.id.tv_keep_days:
            //    keepday();
              //  break;
          //  case R.id.tv_min_everyday:
            //    minEveryDay();
             //   break;
            case R.id.tv_next:
                forwordFollow();
                break;
            default:
                break;
        }

    }
}
